package com.liu.wode.controller;

import com.liu.wode.mapper.OrderMapper;
import com.liu.wode.mapper.RoomSortMapper;
import com.liu.wode.mapper.RoomsMapper;
import com.liu.wode.mapper.UserMapper;
import com.liu.wode.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class OpenRoomController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoomsMapper roomsMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RoomSortMapper roomSortMapper;
    @GetMapping("/OpenRoom")
    public String OpenRoom(Model model){
        List<User> users = userMapper.queryUserList();
        model.addAttribute("users",users);
        return "OpenRoom/selectuser";
    }

    @PostMapping("/FindUser")
    public String FindUser(Model model,@RequestParam("name") String name){
        List<User> users = userMapper.MoHuUser(name);
        model.addAttribute("users",users);
        System.out.println(users);
        return "OpenRoom/selectuser";
    }

    @GetMapping("/Open/{id}")
    public String toUpdateUser(@PathVariable("id")Integer id, Model model){
        User user = userMapper.queryUserById(id);
        List<RoomSort> roomSorts = roomSortMapper.queryRoomSortList();
        List<Rooms> rooms = roomsMapper.queryRoomsBySort("单人房");
        model.addAttribute("user",user);
        model.addAttribute("roomSorts",roomSorts);
        model.addAttribute("rooms",rooms);
        System.out.println(rooms);
        return "OpenRoom/addorder";
    }
    @PostMapping("/FindRoom")
    public String TOAddOrder(@RequestParam("userid")Integer id,@RequestParam("roomsort")String roomsort,Model model ){
        User user = userMapper.queryUserById(id);
        List<RoomSort> roomSorts = roomSortMapper.queryRoomSortList();
        List<Rooms> rooms = roomsMapper.queryRoomsBySort(roomsort);
        model.addAttribute("user",user);
        model.addAttribute("roomSorts",roomSorts);
        model.addAttribute("rooms",rooms);
        System.out.println(rooms);
        return "OpenRoom/addorder";
    }

    @RequestMapping("/Troom")
    public String ChangActive(@RequestParam("R")Integer Rid,@RequestParam("U")Integer Uid){
        int init=0;
        while (orderMapper.queryOrderById(init)!=null){
            init++;
        }
        User user = userMapper.queryUserById(Uid);
        Rooms rooms = roomsMapper.queryRoomsById(Rid);
        orderMapper.addOrder(new Order(init,user.getName(),rooms.getSort(),rooms.getNumber(),new Date(),null));
        roomsMapper.reduceSum(Rid);
        return "redirect:/OpenRoom";
    }

    @GetMapping("/cancel/{id}")
    public String cancel(@PathVariable("id")Integer id, Model model){
        User user = userMapper.queryUserById(id);
        List<Order> orders = orderMapper.MoHuOrder(user.getName());
        model.addAttribute("orders",orders);
        return "order/Olist";
    }
}
